const app = getApp()
//新增
// 引入SDK核心类，根据自己放的路径来写这个SDK核心类的位置
// var QQMapWX = require('../../qqmap-wx-jssdk.min');
// var qqmapsdk = new QQMapWX({
//   key: 'INUBZ-UKHCJ-KS5FI-FDQMU-TXCXV-ZHFSH' // 必填，填自己在腾讯位置服务申请的key
// });
Page({

  /**
   * 页面的初始数据
   */
  data: {
    place: ["广东省", "广州市", ""],
    phaseArray: ["时间排序", "离我最近", "人气最高"],
    phaseArray: ["时间排序", "离我最近", "人气最高"],
    sortingArray: ["全部", "施工中", "已完成"],
    phaseIndex: '',
    sortingIndex: '',
    index: 4,
    // 第1页
    pageNo: 1,
    // 一页有10条数据
    pageSize: 10,
    projectList:[],
    latitude:"",
    longitude:""
  },

  bindtype(e) {
    console.log(e.currentTarget.dataset.index)
    this.setData({
      index: e.currentTarget.dataset.index
    })
  },
  // 取消选择
  bindcancel() {
    this.setData({
      index: 4
    })
  },

  // 跳转项目详情
  gositeDetail(e) {
    wx.navigateTo({
      url: '/pages/siteDetails/siteDetails?orderId='+e.currentTarget.dataset.id,
    })
  },
  // 选择城市
  bindRegionChangePlace(e) {
    console.log(e)
    this.setData({
      place: e.detail.value,
      index: 4
    })
    this.getBricklayList()
  },
  // 选择排序
  bindPickerChangePhase(e) {
    let t = this
    if (e.detail.value == 1) {
      wx.getSetting({
        success(res) {
          let authSetting = res.authSetting
          console.log(authSetting)
          if (res.authSetting['scope.userLocation'] == false) {
            wx.showModal({
              title: '提示',
              title: '您拒绝了定位权限,此功能需要授权定位,请授权',
              success() {
                wx.openSetting({
                  success(res) {
                    console.log(res.authSetting)
                  }
                })
              }
            })
            t.setData({
              index: 4
            })
          } else {
            t.setData({
              phaseIndex: e.detail.value,
              index: 4
            })
            t.getBricklayList()
          }

        }
      })

    } else {
      this.setData({
        phaseIndex: e.detail.value,
        index: 4
      })
      this.getBricklayList()
    }
   
  },
  // 显示状态
  bindPickerChangeSorting(e) {
    console.log(e)
    this.setData({
      sortingIndex: e.detail.value,
      index: 4
    })
    this.getBricklayList()
  },
  // 全民监工接口
  getBricklayList() {
    let projectStatus 
    // this.data.sortingIndex == '' ? '' : t.data.sortingArray[sortingIndex]
    if (this.data.sortingIndex == ''||this.data.sortingIndex==0) {
      projectStatus=''
    }else{
      projectStatus=this.data.sortingIndex
    }



    let selectSort
    if (this.data.phaseIndex == '') {
      selectSort=''
    }else{
     if(this.data.phaseIndex == 0){
      selectSort='time '
     }else if(this.data.phaseIndex == 1){
      selectSort='distance'
     }else if(this.data.phaseIndex == 2){
      selectSort='visit '
     }
    }




    let t = this

    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })

    wx.request({
      url: app.globalData.Interfaceurl + 'getOverseerPage',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        projectProvince: t.data.place[0],
        projectCity: t.data.place[1],
        projectArea: t.data.place[2],
        projectLongitude:t.data.latitude,
        projectLatitude:t.data.longitude,
        projectStatus: projectStatus,
        selectSort: selectSort,
        pageNo:t.data.pageNo,
        pageSize:t.data.pageSize
      },
      success: res => {
        wx.hideLoading();
        console.log(res)
        let projectList = t.data.projectList
        if (t.data.pageNo == 1) {
          projectList = res.data.page.list
        } else {
          projectList.push(...res.data.page.list)
        }
        t.setData({
          projectList: projectList,
          projectlast: res.data.page.last
        })
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var t = this;
    wx.getLocation({
      success(res) {
        t.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })

      }
    })
    this.setData({
      Imageurl: app.globalData.Imageurl
    })
    this.getBricklayList()
  },
    /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.pageNo >= this.data.projectlast) {
      return
    }
    this.setData({
      pageNo: this.data.pageNo+1
    })
 this.getOrderBricklayPage()
  },
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },





  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})